package com.axend.aerosense.room.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import com.axend.aerosense.common.R;
import com.axend.aerosense.common.databinding.CommonFragmentHeightpickerLayoutBinding;
import com.axend.aerosense.common.ui.HeightPicker.HeightConfig;
import com.axend.aerosense.common.ui.HeightPicker.HeightRepository;
import com.axend.aerosense.common.ui.HeightPicker.HeightWheel;
import com.axend.aerosense.room.entity.Height;
import com.jzxiang.pickerview.adapters.NumericWheelAdapter;
import com.jzxiang.pickerview.config.NumberConfig;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

public class BottomHeightPickerDialog extends DialogFragment implements View.OnClickListener {

    private HeightWheel heightWheel;
    private NumericWheelAdapter mNumberAdapter;
    private HeightConfig mHeightConfig;
    private HeightRepository mRepository;;
    private CommonFragmentHeightpickerLayoutBinding dialogBinding;


    private static BottomHeightPickerDialog newIntance(HeightConfig heightConfig) {
        BottomHeightPickerDialog timePickerDialog = new BottomHeightPickerDialog();
        timePickerDialog.initialize(heightConfig);
        return timePickerDialog;
    }

    private void initialize(HeightConfig heightConfig) {
        mHeightConfig = heightConfig;
        mRepository = new HeightRepository(mHeightConfig);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加这句话去掉自带的标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.common_fragment_heightpicker_layout, container, false);
        return dialogBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void onResume() {
        super.onResume();
        int height = getResources().getDimensionPixelSize(com.jzxiang.pickerview.R.dimen.picker_height);

        Window window = getDialog().getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, height);//Here!
        window.setGravity(Gravity.BOTTOM);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    void initView() {
        heightWheel = new HeightWheel(dialogBinding.getRoot(), mHeightConfig);
        dialogBinding.commonSure.setOnClickListener(v->sureClicked());
        dialogBinding.commonCancel.setOnClickListener(v->dismiss());
//        heightWheel.addChangingListener(numberListener);
//        initData();
    }
//
//    void initData() {
//        int minValue = mRepository.getMinValue();
//        int maxValue = mRepository.getMaxValue();
//
//        mNumberAdapter = new NumericWheelAdapter(getContext(), minValue, maxValue, PickerContants.FORMAT_0, mNumberConfig.mUnit, mNumberConfig.step);
//        mNumberAdapter.setConfig(mNumberConfig);
//        wheelView.setViewAdapter(mNumberAdapter);
//        wheelView.setCurrentItem(mRepository.getDefaultValue() - minValue);
//    }
//
//    public int getCurrentValue() {
//        return wheelView.getCurrentItem() * mRepository.getStep() + mRepository.getMinValue();
//    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == com.jzxiang.pickerview.R.id.tv_cancel) {
            dismiss();
        } else if (i == com.jzxiang.pickerview.R.id.tv_sure) {
            sureClicked();
        }
    }

    /*
    * @desc This method is called when onClick method is invoked by sure button. A Calendar instance is created and 
    *       initialized. 
    * @param none
    * @return none
    */
    void sureClicked() {
        Height height = new Height(heightWheel.getCurrentFoot(), heightWheel.getCurrentInch());
        if (mHeightConfig.getFtNumberConfig().mCallBack != null) {
            mHeightConfig.getFtNumberConfig().mCallBack.onDateSet(this, height);

        }else if (mHeightConfig.getInNumberConfig().mCallBack != null) {
            mHeightConfig.getInNumberConfig().mCallBack.onDateSet(this, height);
        }
        dismiss();
    }

    public static class Builder {
        private HeightConfig mPickerConfig;

        public Builder() {
            mPickerConfig = new HeightConfig(new NumberConfig(), new NumberConfig());
        }

        public Builder setThemeColor(int footColor) {
            mPickerConfig.getFtNumberConfig().mThemeColor = footColor;
            mPickerConfig.getInNumberConfig().mThemeColor = footColor;
            return this;
        }

        public Builder setType(Type footType, Type inchType) {
            mPickerConfig.getFtNumberConfig().mType = footType;
            mPickerConfig.getInNumberConfig().mType = inchType;
            return this;
        }

        public Builder setWheelItemTextSize(int footSize, int inchSize) {
            mPickerConfig.getFtNumberConfig().mWheelTVSize = footSize;
            mPickerConfig.getInNumberConfig().mWheelTVSize = inchSize;
            return this;
        }

        public Builder setCyclic(boolean footCyclic, boolean inchCyclic) {
            mPickerConfig.getFtNumberConfig().cyclic = footCyclic;
            mPickerConfig.getInNumberConfig().cyclic = inchCyclic;
            return this;
        }

        public Builder setMinValue(int footValue, int inchValue) {
            mPickerConfig.getFtNumberConfig().mMinValue = footValue;
            mPickerConfig.getInNumberConfig().mMinValue = inchValue;
            return this;
        }

        public Builder setMaxValue(int footValue, int inchValue) {
            mPickerConfig.getFtNumberConfig().mMaxValue = footValue;
            mPickerConfig.getInNumberConfig().mMaxValue = inchValue;
            return this;
        }

        public Builder setDefValue(int footValue, int inchValue) {
            mPickerConfig.getFtNumberConfig().mDefValue = footValue;
            mPickerConfig.getInNumberConfig().mDefValue = inchValue;
            return this;
        }

        public Builder setUnit(String footUnit, String inchUnit) {
            mPickerConfig.getFtNumberConfig().mUnit = footUnit;
            mPickerConfig.getInNumberConfig().mUnit = inchUnit;
            return this;
        }

        public Builder setCallBack(OnDateSetListener<Height> listener) {
            mPickerConfig.getFtNumberConfig().mCallBack = listener;
            mPickerConfig.getInNumberConfig().mCallBack = listener;
            return this;
        }

        public BottomHeightPickerDialog build() {
            return newIntance(mPickerConfig);
        }

    }
}
